package com.iengoo.inductionms.ctrl.rest;

import com.iengoo.inductionms.config.ExcelConfig;
import com.iengoo.inductionms.service.ExportService;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * @author zpym
 * @CopyRight (C) http://www.iengoo.com
 * @brief
 * @email 122162005@qq.com
 * @date 2019-03-10 22:11
 */
@RestController
@RequestMapping("/export")
public class ExportController {
    @Autowired
    private ExcelConfig excelConfig;
    @Autowired
    private ExportService exportService;

    @GetMapping("/{type}")
    public void download(@PathVariable String type, HttpServletResponse resp) throws IOException {
//        try (InputStream is = new FileInputStream(new File("path" + id + "jpg"));
        if ("entry".equals(type)) {
            exportService.eduExport(type);
        } else if ("salary".equals(type)) {
            exportService.payExport(type);
        } else if ("edu".equals(type)) {
            exportService.eduExport(type);
        } else if ("account".equals(type)) {
            exportService.accountExport(type);
        }
        try (InputStream is = new FileInputStream(new File(excelConfig.getExcelPath() + type + ".xls"));
             OutputStream os = resp.getOutputStream()) {
            resp.setContentType("application/x-download");
            resp.addHeader("Content-Disposition", "attachment;filename=" + type + ".xls");
            IOUtils.copy(is, os);
            os.flush();
        }
    }
}
